<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 7/11/17
 * Time: 9:05 AM
 */
declare(strict_types=1);


namespace application\module\admin\controller;

use application\module\admin\addon\Menu as M;
use wshore\core\Response;
use wshore\throws\DatabaseException;
use wshore\throws\db\RecordNotFoundException;


class menu extends admin
{
    /**
     * 显示页面
     */
    public function index()
    {
        $this->doLayoutAssign();
        $this->display();
    }

    /**
     * 获取格式化后的菜单列表
     */
    public function getlist()
    {
        try {
            Response::success('获取成功', M::getInstance()->getlist(false));
        } catch (DatabaseException $e) {
            Response::success('获取失败:' . $e->getMessage());
        }
    }

    /**
     * 删除菜单项
     * @param int $id
     */
    public function delete(int $id)
    {
        try {
            $model = M::getInstance();
            Response::success("删除成功", $model->delete($id));
        } catch (RecordNotFoundException $exception) {
            Response::failure("删除失败：无法找到id为‘{$id}’的菜单");
        } catch (DatabaseException $e) {
            Response::failure("删除失败：" . $e->getMessage());
        }
    }

    /**
     * 修改菜单项
     * @param $id
     * @param $name
     * @param $value
     * @param $pid
     * @param $icon
     * @param $category
     * @param $orderNo
     * @param string $target
     */
    public function update($id, $name, $value, $pid, $icon, $category, $orderNo, $target = '_self')
    {
        try {
            $info = M::getInstance()->update($id, $name, $value, $pid, $icon, $category, $orderNo, $target);
            Response::success('修改成功', $info);
        } catch (RecordNotFoundException $e) {
            Response::failure("修改失败：无法找到id为‘{$id}’的菜单");
        } catch (DatabaseException $exception) {
            Response::failure("修改失败：" . $exception->getMessage());
        }
    }

    /**
     * 添加菜单项
     * @param string $name
     * @param string $value
     * @param $pid
     * @param string $icon
     * @param $category
     * @param $orderNo
     * @param string $target
     */
    public function add(string $name, string $value, $pid, string $icon, $category, $orderNo, $target = '_self')
    {
        try {
            Response::success('添加成功', M::getInstance()->add($name, $value, $pid, $icon, $category, $orderNo, $target));
        } catch (DatabaseException $exception) {
            Response::failure("修改失败：" . $exception->getMessage());
        }
    }
}